package tomcat;

import javax.servlet.*;
import java.io.IOException;

public class test01 implements Servlet{
    private String uname;
    private String pwd;
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        //获取xml中init-param的value
        this.uname = servletConfig.getInitParameter("uname");
        this.pwd = servletConfig.getInitParameter("pwd");

        //全局的初始化的参数
        ServletContext context = servletConfig.getServletContext();
        String conStr = context.getInitParameter("uname");
        System.out.println(conStr);
        //获取tomcat信息
        String serverInfo = context.getServerInfo();
        System.out.println(serverInfo);
        //获取当前web应用名
        String contextPath = context.getContextPath();
        System.out.println(contextPath);
        //获取当前web应用的绝对路径
        String path = context.getRealPath("");
        System.out.println(path);
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
      //获取传入的参数
       String uname = servletRequest.getParameter("uname");
       String pwd = servletRequest.getParameter("pwd");
       String msg = "账号或密码错误";
       //比较值是否一致
       if(uname.equals(this.uname)&&pwd.equals(this.pwd)){
           msg = "恭喜登陆成功";
       }
       //向客户端返回数据
       servletResponse.setContentType("text/html;charset=UTF-8");
       servletResponse.getWriter().write(msg);
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
